package com.example.task1.ui.AllService.PublicBenefit;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.task1.R;
import com.example.task1.application.MyApplication;
import com.example.task1.bean.PublicRecommendBean;
import com.example.task1.http.Api;
import com.example.task1.ui.BaseAdapter;
import com.example.task1.util.ApiResp;
import com.example.task1.util.CallBack;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;

public class RecommendListAdapter extends BaseAdapter<PublicRecommendBean.RowsDTO> {

    private ImageView image;
    private TextView tvTitle;
    private TextView tvName;
    private TextView tvTotal;
    private TextView tvNow;
    private TextView tvCount;

    private List<PublicRecommendBean.RowsDTO> data = new ArrayList<>();
    private Context context;
    private Button btnDonation;

    public RecommendListAdapter(Context context) {
        this.context = context;
    }

    public void setData(List<PublicRecommendBean.RowsDTO> rows) {
        int i = data.size();
        data.addAll(rows);
        notifyItemInserted(i);
    }

    @Override
    protected int getLayout() {
        return R.layout.publicrecomment;
    }

    @Override
    protected Context getContext() {
        return context;
    }

    @Override
    protected List<PublicRecommendBean.RowsDTO> getData() {
        return data;
    }

    @Override
    protected void onBind(myHolder holder, PublicRecommendBean.RowsDTO date) {
        MyApplication.glide(image, date.getImgUrl());
        tvCount.setText("捐款次数：" + date.getDonateCount());
        tvName.setText(date.getAuthor());
        tvNow.setText("以筹善款：" + date.getMoneyNow());
        tvTitle.setText(date.getName());
        tvTotal.setText("总计所需捐款：" + date.getMoneyTotal());
        btnDonation.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                AlertDialog.Builder builder = new AlertDialog.Builder(context);
                builder.setTitle("输入捐款金额");
                EditText editText = new EditText(context);
                editText.setHint("金额");
                builder.setView(editText);
                builder.setNegativeButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        String money = editText.getText().toString();
                        if (TextUtils.isEmpty(money)) {
                            MyApplication.show("金额不能为空");
                        } else {
                            HashMap<String, Object> map = new HashMap<>();
                            map.put("activityId", date.getId());
                            map.put("donateMoney", money);
                            Call<ApiResp> post = Api.api().post(MyApplication.TOKEN, "/prod-api/api/public-welfare/donate-record", Api.getBody(map));
                            Api.send(post, ApiResp.class, new CallBack<ApiResp>() {
                                @Override
                                public void success(ApiResp data) {
                                    MyApplication.show("捐款成功");
                                }

                                @Override
                                public void error(String msg) {

                                }
                            });
                        }
                    }
                });

                builder.setPositiveButton("取消", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {

                    }
                });
                builder.show();
            }
        });
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                MyApplication.intent(context, PublicDetailsActivity.class, date.getId());
            }
        });
    }

    @Override
    protected void holder(View itemView) {
        initView(itemView);
    }

    private void initView(View itemView) {
        image = (ImageView) itemView.findViewById(R.id.image);
        tvTitle = (TextView) itemView.findViewById(R.id.tv_title);
        tvName = (TextView) itemView.findViewById(R.id.tv_name);
        tvTotal = (TextView) itemView.findViewById(R.id.tv_total);
        tvNow = (TextView) itemView.findViewById(R.id.tv_now);
        tvCount = (TextView) itemView.findViewById(R.id.tv_count);
        btnDonation = (Button) itemView.findViewById(R.id.btn_donation);
    }
}
